
#include "mainwindow.h"

#include <QComboBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGroupBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QVBoxLayout>

#include <QListWidget>
#include <QScrollArea>

#include <QuickTemplateObject/QuickTemplateObject.h>
#include <QtPrivateDeclar/QtPrivateDeclar.h>

//#include <QtWebEngineWidgets/QtWebEngineWidgets>

MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
    QuickTemplateObject *manager = new QuickTemplateObject;
	QHBoxLayout *layout = new QHBoxLayout(this);
	
    QVBoxLayout *Leftlayout = new QVBoxLayout();
// left

// 1.1
    QGroupBox *leftBox = new QGroupBox("查询");
    QVBoxLayout *leftBoxLayout = new QVBoxLayout(leftBox);
    Leftlayout->addWidget(leftBox);

    QHBoxLayout *searchBoxLayout = new QHBoxLayout();
    QLabel *searchLabel = new QLabel("关键词:");
    QLineEdit *searchEdit = new QLineEdit;
    searchBoxLayout->addWidget(searchLabel);
    searchBoxLayout->addWidget(searchEdit);

    QListWidget *cpuListWidget = new QListWidget;
    // QWeb *cpuListWidget = new QtWebView;
    // QWebEngineView *cpuListWidget = new QWebEngineView;
    // cpuListWidget->setUrl(QUrl("https://www.bmcx.com/apiiframe/?api_from=bmcx&api_url=http://cpu.bmcx.com/&api_width=98%&api_backgroundcolor=FFFFFF"));
    


    leftBoxLayout->addLayout(searchBoxLayout);
    leftBoxLayout->addWidget(cpuListWidget);

    QVBoxLayout *Rightlayout = new QVBoxLayout();
// right

// 2.1
    QGroupBox *rightBox = new QGroupBox("比较记录");
    QVBoxLayout *rightBoxLayout = new QVBoxLayout(rightBox);
    Rightlayout->addWidget(rightBox);

// 2.2
    QLabel *title = new QLabel("选中项排名");
    rightBoxLayout->addWidget(title, 0, Qt::AlignTop|Qt::AlignHCenter);

// 2.3
    QListWidget *listWidget = new QListWidget;
    rightBoxLayout->addWidget(listWidget);
    
    layout->addLayout(Leftlayout,1);
    layout->addLayout(Rightlayout,1);
    resize(1200, 800);

// ------

    connect(searchEdit, &QLineEdit::textChanged, [=](const QString &text) {
        cpuListWidget->clear();
        for (auto &obj : manager->m_list)
        {
            bool found = true;

            for (auto &str : text.split(" "))
            {
                if (!obj->getMing_cheng().contains(str)) {
                    found = false;
                }
            }

            if (found) {
                QString item = QString("%1: %2").arg(obj->getPai_ming()).arg(obj->getMing_cheng());
                cpuListWidget->addItem(item);
                continue;
            }
        }
    });

    connect(manager, &QuickTemplateObject::loaded, [=](QList<QtPrivateDeclar *> m_list){
        for (auto &obj : m_list)
        {
            QString item = QString("%1: %2").arg(obj->getPai_ming()).arg(obj->getMing_cheng());
            cpuListWidget->addItem(item);
        }
    });


    QStringList *history = new QStringList();

    connect(cpuListWidget, &QListWidget::itemDoubleClicked, [=](QListWidgetItem *item){
        history->append(item->text());

        listWidget->clear();
        listWidget->addItems(*history);
    });

    connect(listWidget, &QListWidget::itemDoubleClicked, [=](QListWidgetItem *item){
        history->removeAll(item->text());

        listWidget->clear();
        listWidget->addItems(*history);
    });

}
MainWindow::~MainWindow()
{
	
}
